package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.BoutiqueModle;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.tools.ImageUtils;

import java.util.List;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/21
 * 作用: ******
 * 备注: 学院精品课程的adapter
 */
public class BoutiqueFragmentAdapter extends BaseRecyclerAdapter<BoutiqueFragmentAdapter.BoutiqueAdapterViewHolder> {
    private Context context;
    private List<BoutiqueModle> modles;

    public BoutiqueFragmentAdapter(Context context, List<BoutiqueModle> modles) {
        this.context = context;
        this.modles = modles;
    }

    @Override
    public BoutiqueAdapterViewHolder getViewHolder(View view) {
        return new BoutiqueAdapterViewHolder(view);
    }

    @Override
    public BoutiqueAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_boutique_adapter_item,parent,false);
        return new BoutiqueAdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BoutiqueAdapterViewHolder holder, int position, boolean isItem) {
        BoutiqueModle modle=modles.get(position);
        holder.tv_title.setText(modle.getTitle());
        holder.tv_date.setText(modle.getData());
        holder.tv_price.setText(modle.getPrice());
        holder.id=modle.getId();
        int state=modle.getState();
        if(state==0){
            holder.img_topleft.setImageResource(R.mipmap.playstatepicon1);
        }else if(state==1){
            holder.img_topleft.setImageResource(R.mipmap.playstatepicon2);
        }else if(state==2){
            holder.img_topleft.setImageResource(R.mipmap.playstatepicon3);
        }
        ImageUtils.getInstance().ImageStart(context,modle.getImage(),holder.image_rl);
        holder.image_rl.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.webview=modle.getWebview();
    }

    @Override
    public int getAdapterItemCount() {
        return modles.size();
    }

    class BoutiqueAdapterViewHolder extends RecyclerView.ViewHolder{
         private TextView tv_title,tv_date,tv_price;
         private ImageView img_topleft,image_rl;
         private String webview,id;

         public BoutiqueAdapterViewHolder(View itemView) {
             super(itemView);
             tv_title= (TextView) itemView.findViewById(R.id.tv_title);
             tv_date= (TextView) itemView.findViewById(R.id.tv_date);
             tv_price= (TextView) itemView.findViewById(R.id.tv_price);

             img_topleft= (ImageView) itemView.findViewById(R.id.img_topleft);
             image_rl= (ImageView) itemView.findViewById(R.id.image_rl);

             /**
              * item点击事件
              */
             itemView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     onItemClickListener.OnItemClick(itemView,getLayoutPosition()-1);
                 }
             });
         }
    }


    /**
     * 单行点击事件接口
     */
    public interface OnItemClickListener{
        void OnItemClick(View view,int position);
    }

    /**
     * item单机事件外部调用方法
     */
    public OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }


    /*
    清空适配器
     */
    public void clear(){
        clear(modles);
    }

    /**
     * 刷新数据重新添加数据
     * @param modlem
     * @param position
     */
    public void insert(BoutiqueModle modlem,int position){
        insert(modles,modlem,position);
    }
}
